<template>
  <div class="detail">
    <div class="container">
      <!-- 面包屑 -->
      <div class="breade-container">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item
            :to="{ path: `/category/${detailGoodsList.categories?.[1].id}` }"
            >{{ detailGoodsList.categories?.[1].name }}</el-breadcrumb-item
          >
          <el-breadcrumb-item
            :to="{
              path: `/category/sub/${detailGoodsList.categories?.[0].id}`,
            }"
            >{{ detailGoodsList.categories?.[0].name }}</el-breadcrumb-item
          >
          <el-breadcrumb-item>抓绒保暖,毛毛虫儿童运动鞋</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media">
              <!-- 图片预览区 -->
              <ImageView :imageList="detailGoodsList.mainPictures" />
              <!-- 统计数量  -->
              <ul class="goods-sales">
                <li>
                  <p>销量人气</p>
                  <p>{{ detailGoodsList.salesCount }}+</p>
                  <p><i class="iconfont icon-xiaoshou"></i>销量人气</p>
                </li>
                <li>
                  <p>商品评价</p>
                  <p>{{ detailGoodsList.commentCount }}+</p>
                  <p><i class="iconfont icon-chakanpinglun"></i>查看评价</p>
                </li>
                <li>
                  <p>收藏人气</p>
                  <p>{{ detailGoodsList.collectCount }}+</p>
                  <p><i class="iconfont icon-shoucang"></i>收藏商品</p>
                </li>
                <li>
                  <p>品牌信息</p>
                  <p>{{ detailGoodsList.brand?.name }}+</p>
                  <p><i class="iconfont icon-pinzhibaozhang"></i>品牌主页</p>
                </li>
              </ul>
            </div>
            <div class="spec">
              <!-- 商品信息区 -->
              <h3 class="g-name">{{ detailGoodsList.name }}</h3>
              <p class="g-desc">{{ detailGoodsList.desc }}</p>
              <div class="g-price">
                <span>¥{{ detailGoodsList.oldPrice }}</span>
                <span>¥{{ detailGoodsList.price }}</span>
              </div>
              <div class="g-service">
                <dl>
                  <dt>促销</dt>
                  <dd>12月好物放送，APP领券购买直降120元</dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>无忧退货</span>
                    <span>快速退款</span>
                    <span>免费包邮</span>
                    <a href="javascript:;">了解详情</a>
                  </dd>
                </dl>
              </div>
              <!-- sku组件 -->
              <Sku :goods="detailGoodsList" @change="skuChange" />
              <!-- 数据组件 -->
              <el-input-number v-model="count" :min="1" @change="countChange" />
              <!-- 按钮组件 -->
              <div>
                <el-button size="large" class="shopBtn" @click="addCart"
                  >加入购物车</el-button
                >
              </div>
            </div>
          </div>
          <div class="goods-footer">
            <div class="goods-article">
              <!-- 商品详情 -->
              <div class="goods-tabs">
                <nav>
                  <h3>商品详情</h3>
                </nav>
                <!-- 属性 -->
                <div class="goods-detail">
                  <ul class="attrs">
                    <li
                      v-for="item in detailGoodsList.details?.properties"
                      :key="item.value"
                    >
                      <span class="dt">{{ item.name }}</span>
                      <span class="dd">{{ item.value }}</span>
                    </li>
                  </ul>
                  <!-- 图片 -->
                  <img
                    v-for="img in detailGoodsList.details?.pictures"
                    :src="img"
                    :key="img"
                  />
                </div>
              </div>
            </div>
            <!-- 24热榜+专题推荐 -->
            <div class="goods-aside">
              <!-- 24小时榜 -->
              <DetailHot :hotType="1"></DetailHot>
              <!-- 周榜 -->
              <DetailHot :hotType="2"></DetailHot>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineOptions, ref, onMounted } from "vue";
//引入API
import { reqDetailGoods } from "@/apis/detail";
//引入子组件
import DetailHot from "./components/DetailHot.vue";
//引入购物车仓库
import { useCartStore } from "@/stores/modules/shoppingCartStore";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
defineOptions({
  name: "Detail",
});
onMounted(() => {
  getDetailGoods();
});
//应用仓库
const cartStore = useCartStore();
let router = useRoute();
//获取详情页的商品数据
let detailGoodsList = ref({});
//购物车数字输入框
let count = ref(1);
//收集sku的数据
let skuObj = {};
//获取详情页的商品数据的方法
const getDetailGoods = async () => {
  let res: any = await reqDetailGoods(router.params.id);
  detailGoodsList.value = res.result;
};
//sku规格操作的回调
const skuChange = (sku) => {
  console.log(sku);
  skuObj = sku;
};
//购物车数字输入框更改时的回调；value 输入框内容值
const countChange = (value) => {
  console.log(value);
  // count.value = value;
};
//点击添加购物车回调
const addCart = () => {
  if (skuObj.skuId) {
    //规格已经选择 触发action
    cartStore.addCart({
      id: detailGoodsList.value.id,
      name: detailGoodsList.value.name,
      picture: detailGoodsList.value.mainPictures[0],
      price: detailGoodsList.value.price,
      count: count.value,
      skuId: skuObj.skuId,
      attrsText: skuObj.specsText,
      selected: true,
    });
  } else {
    // 规格未选择  提示用户
    ElMessage.warning("请选择规格");
  }
};
</script>

<style scoped lang="scss">
.detail {
  /** 面包屑*/
  .breade-container {
    padding: 25px 0;
  }
  /**商品信息 */
  .goods-info {
    display: flex;
    min-height: 600px;
    background-color: #fff;
    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
      .goods-sales {
        display: flex;
        width: 400px;
        text-align: center;
        justify-content: space-between;
        li {
          flex: 1;
          position: relative;
          ~ li::after {
            position: absolute;
            top: 10px;
            left: 0;
            height: 60px;
            border-left: 1px solid #e4e4e4;
            content: "";
          }
          p {
            &:first-child {
              color: #999;
            }
            &:nth-child(2) {
              color: $priceColor;
              margin-top: 10px;
            }
            i {
              margin: 0 5px;
            }
            &:hover {
              color: $xtxColor;
              cursor: pointer;
            }
          }
        }
      }
    }
    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
      .g-name {
        font-size: 22px;
      }
      .g-desc {
        color: #999;
        margin-top: 10px;
      }
      .g-price {
        span {
          &:first-child {
            color: $priceColor;
            font-size: 22px;
            margin-right: 20px;
          }
          &:last-child {
            color: #999;
            text-decoration: line-through;
            font-size: 16px;
          }
        }
      }
      .g-service {
        width: 500px;
        margin-top: 10px;
        background-color: #f5f5f5;
        dl {
          padding: 10px;
          display: flex;
          dt {
            width: 50px;
            color: #999;
          }
          dd {
            color: #666;
            span {
              margin: 0 10px;
              &::before {
                content: "•";
                color: $xtxColor;
                margin-right: 2px;
              }
            }
            a {
              color: $xtxColor;
            }
          }
        }
      }
    }
    .shopBtn {
      margin-top: 20px;
    }
  }
  /**商品详情 */
  .goods-footer {
    display: flex;
    margin-top: 20px;
    .goods-article {
      width: 940px;
      margin-right: 20px;
      .goods-tabs {
        h3 {
          padding: 20px;
          font-size: 30px;
        }
        min-height: 600px;
        background: #fff;
        .goods-detail {
          padding: 40px;
          .attrs {
            display: flex;
            flex-wrap: wrap;
            margin-bottom: 30px;
            li {
              display: flex;
              width: 50%;
              margin-bottom: 10px;
              .dt {
                width: 100px;
                color: #999;
              }

              .dd {
                flex: 1;
                color: #666;
              }
            }
          }
          img {
            width: 100%;
          }
        }
      }
    }
    .goods-aside {
      width: 280px;
      min-height: 1000px;
    }
  }
}
</style>
